package com.maxjay.component.simplelist.web.page;

import com.maxjay.system.constant.SystemParamKeys;

public class Page {
	private int pageIndex;
	private int pageSize = SystemParamKeys.PAGE_SIZE;
	private int pageCount;
	private int rowCount;
	private boolean isRowCountInit = false;

	private int startRowIndex;
	private int endRowIndex;
	
	public Page() {
		pageIndex = 1;
	}

	public Page(int rowCount) {
		this.pageIndex = 1;
		this.rowCount = rowCount;
		this.isRowCountInit = true;
		refresh();
	}
	
	public Page(int pageIndex, int rowCount){
		this.pageIndex = pageIndex;
		this.rowCount = rowCount;
		this.isRowCountInit = true;
		refresh();
	}
	
	public void refresh() {
		this.pageCount = rowCount % pageSize == 0 ? (rowCount / pageSize) : (rowCount / pageSize + 1);
		this.startRowIndex = pageSize * (getPageIndexInt() - 1);
		this.endRowIndex = getPageIndexInt() == pageCount ? rowCount : getPageIndexInt() * pageSize;
	}

	public int getStartRowIndex() {
		return startRowIndex;
	}

	public int getEndRowIndex() {
		return endRowIndex;
	}

	public int getPageIndexInt() {
		try {
			int pageIndexInt = Integer.valueOf(pageIndex);
			if(pageIndexInt < 1) {
				this.pageIndex = 1;
			}
			return pageIndex;
		} catch (NumberFormatException e) {
			this.pageIndex = 1;
			return 1;
		}
	}
	
	public String getPageIndex() {
		return String.valueOf(pageIndex);
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getPageCount() {
		return pageCount;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
		this.isRowCountInit =true;
		this.refresh();
	}
	
	public void setRowCountAndPageIndex(int rowCount, int pageIndex) {
		this.rowCount = rowCount;
		this.pageIndex = pageIndex;
		this.isRowCountInit =true;
		this.refresh();
	}

	public void setStartRowIndex(int startRowIndex) {
		this.startRowIndex = startRowIndex;
	}

	public void setEndRowIndex(int endRowIndex) {
		this.endRowIndex = endRowIndex;
	}

	public boolean isRowCountInit() {
		return isRowCountInit;
	}

	public void setRowCountInit(boolean isRowCountInit) {
		this.isRowCountInit = isRowCountInit;
	}

}
